package com.filetransfer.view.Components;
import com.filetransfer.utils.SvgIconUtil;
import com.filetransfer.viewmodel.base.TaskTableModel;
import com.filetransfer.viewmodel.base.TaskTableRowModel;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
@Getter
public abstract class TaskPanel<T extends TaskTableModel, TRow extends TaskTableRowModel> extends JPanel {
    private final JTable taskTable;
    private final T viewModel;
    private final JLabel messageLabel;
    private final JButton actionButton;
    private final JButton optionalButton; // 新增的按钮
    public TaskPanel(String placeholderMessage, String buttonIconPath, T viewModel) {
        // 使用 BorderLayout 管理布局
        setLayout(new BorderLayout());
        // 顶部信息区域
        messageLabel = new JLabel(placeholderMessage);
        messageLabel.setHorizontalAlignment(SwingConstants.LEFT);
        messageLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        add(messageLabel, BorderLayout.NORTH);
        // 中间叠加布局区域
        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.setLayout(null);
        add(layeredPane, BorderLayout.CENTER);
        // 表格放在底层
        this.viewModel = viewModel;
        this.taskTable = createTaskTable();
        JScrollPane scrollPane = new JScrollPane(taskTable);
        scrollPane.setBounds(0, 0, 800, 600);
        layeredPane.add(scrollPane, JLayeredPane.DEFAULT_LAYER);
        // 悬浮按钮放在上层
        FlatSVGIcon buttonIcon = SvgIconUtil.createSvgIcon(buttonIconPath, 30, 30);
        actionButton = new JButton(buttonIcon);
        actionButton.setPreferredSize(new Dimension(75, 75));
        actionButton.putClientProperty("JButton.buttonType", "roundRect");
        actionButton.setBounds(700, 500, 75, 75);
        actionButton.setFont(actionButton.getFont().deriveFont(20f));
        layeredPane.add(actionButton, JLayeredPane.PALETTE_LAYER);
        // 新增的可选按钮s
        FlatSVGIcon optionalIcon = SvgIconUtil.createSvgIcon("icons/add_comment.svg", 20, 20);
        optionalButton = new JButton(optionalIcon);
        optionalButton.setPreferredSize(new Dimension(50, 50));
        optionalButton.putClientProperty("JButton.buttonType", "roundRect");
        optionalButton.setFont(optionalButton.getFont().deriveFont(16f));
        optionalButton.setBounds(700, 430, 50, 50);
        optionalButton.setVisible(false);
        layeredPane.add(optionalButton, JLayeredPane.PALETTE_LAYER);
        // 动态调整大小
        layeredPane.addComponentListener(new java.awt.event.ComponentAdapter() {
            @Override
            public void componentResized(java.awt.event.ComponentEvent e) {
                Dimension size = layeredPane.getSize();
                scrollPane.setBounds(0, 0, size.width, size.height);
                // 动态调整 actionButton 和 optionalButton 的位置
                int buttonSize = 75;
                int optionalButtonSize = 50;
                actionButton.setBounds(size.width - buttonSize - 20, size.height - buttonSize - 20, buttonSize, buttonSize);
                optionalButton.setBounds(size.width - optionalButtonSize - 32, size.height - buttonSize - 20 - optionalButtonSize - 10, optionalButtonSize, optionalButtonSize);
            }
        });
        // 按钮动作由子类实现
        actionButton.addActionListener(e -> onActionButtonClick());
        optionalButton.addActionListener(e -> onOptionalButtonClick());
    }
    // 创建表格
    private JTable createTaskTable() {
        JTable taskTable = createJTable(this.viewModel);
        taskTable.getTableHeader().setReorderingAllowed(false);
        taskTable.getTableHeader().setResizingAllowed(false);
        taskTable.getTableHeader().setOpaque(false);
        taskTable.getTableHeader().setBackground(new Color(0, 0, 0, 0));
        taskTable.getTableHeader().setBorder(BorderFactory.createEmptyBorder());
        this.viewModel.editColumnModel(taskTable.getColumnModel());
        return taskTable;
    }
    @NotNull
    private static <T extends TaskTableModel> JTable createJTable(T viewModel) {
        JTable taskTable = new JTable(viewModel);
        taskTable.setCellSelectionEnabled(false); // 禁用单元格选择
        taskTable.setRowHeight(25);
        taskTable.setShowGrid(false);
        taskTable.setOpaque(false);
        taskTable.setBackground(new Color(0, 0, 0, 0));
        taskTable.setBorder(BorderFactory.createEmptyBorder());
        JScrollPane scrollPane = new JScrollPane(taskTable);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        taskTable.setFillsViewportHeight(true);
        taskTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        centerAlignTableContent(taskTable);
        return taskTable;
    }
    private static void centerAlignTableContent(JTable table) {
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
//        for (int i = 0; i < table.getColumnCount(); i++) {
//            table.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
//        }
        table.setDefaultRenderer(Object.class, centerRenderer);
    }
    protected abstract void onActionButtonClick();
    protected void onOptionalButtonClick() {
    }
    protected void addTask(TRow row) {
        viewModel.addTask(row);
    }
    public void setOptionalButtonVisible(boolean visible) {
        optionalButton.setVisible(visible);
    }
}
